import {
  getGoodList,
  getRank,
  createProduct,
  createProductLabel,
  getCategory,
  delProduct,
  getSaleData,
  getSaleWeekData,
  getSaleMonthData,
} from "../api/index";
// 获取到产品列表
export const getGoodListTool = async () => {
  let { data } = await getGoodList({});
  console.log("获取产品列表", data);
  if (!data) {
    return {
      content: [
        {
          type: "text",
          text: "未能检索警报数据",
        },
      ],
    };
  }
  let confText = "";
  data.forEach((item: any) => {
    confText += `名称: ${item.name}, 价格: ${item.price}，跳转链接：http://localhost:3000/goodview/${item.id}\n`;
  });
  return {
    content: [
      {
        type: "text",
        text: confText,
      },
    ],
  };
};
// 获取销售排名
export const geRandListTool = async (params: any) => {
  let { data } = await getRank(params);
  console.log("获取产品列表", data);
  if (!data) {
    return {
      content: [
        {
          type: "text",
          text: "未能检索警报数据",
        },
      ],
    };
  }
  let confText = "";
  data.forEach((item: any, index: any) => {
    confText += `销售排名：第${index + 1}名，产品名: ${item.name}, 已售价: ${
      item.saleMoney
    }\n`;
  });
  return {
    content: [
      {
        type: "text",
        text: confText,
      },
    ],
  };
};
// 获取到模型信息
export const getModelInfoTool = async () => {
  return {
    content: [
      {
        type: "text",
        text: "我的名字叫小果森森，是基于千问大模型实现的一个销售助理小助手，可以帮你查询产品信息和销售排名等信息",
      },
    ],
  };
};
// 获取到商场主营方向
export const getShangChengTool = async () => {
  return {
    content: [
      {
        type: "text",
        text: "欢迎来到奇奇屋！我们提供安全、有趣的童装、玩具、婴童用品及学习文具，是您一站式选购所有孩童用品的最佳选择。",
      },
    ],
  };
};
// 获取联系方式
export const getContactTool = async () => {
  return {
    content: [
      {
        type: "text",
        text: "全部的联系方式为：电话 123-456-7890，邮箱：123456789@613.com",
      },
    ],
  };
};
// 获取联系方式
export const getCreateProductTool = async (params: any) => {
  let productInfo = {
    discount: params.discount,
    name: params.name,
    description: params.description,
    categoryId: params.categoryId,
    imgs: params.imgs,
  };
  try {
    let dataRes = await createProduct(productInfo);
    let id = dataRes.data;
    let categoryList = params.categoryList;
    await createProductLabel(id, categoryList);
    return {
      content: [
        {
          type: "text",
          text: "创建产品成功，请到产品管理或者刷新页面查看",
        },
      ],
    };
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: "创建商品失败，" + error,
        },
      ],
    };
  }
};

// 获取产品分类 /api/category
export const getCategoryTool = async () => {
  let res = await getCategory();
  if (res.code === "200") {
    // let content = "";
    // res.data.forEach((item: any) => {
    //   content += `产品分类id（categoryId）：${item.id},产品分类名称：${item.name}`;
    // });
    return res.data;
  }
  return [];
};
// delProduct
export const delProductTool = async (id: any) => {
  try {
    await delProduct(id);
    return {
      content: [
        {
          type: "text",
          text: "删除成功",
        },
      ],
    };
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: "删除失败" + error,
        },
      ],
    };
  }
};
export const getSaleDateToole = async (token: any) => {
  let aaa = {};
  try {
    let res = await getSaleData(token);
    aaa = res;
    let res1 = await getSaleWeekData(token);
    let res2 = await getSaleMonthData(token);
    let saleData = res.data.categoryIncomes;
    let weekData = res1.data.weekIncome;
    let monthData = res2.data.monthIncome;
    let totalSale = 0;
    let totaldetails = "";
    let weekSale = 0;
    let monthSale = 0;
    saleData.forEach((item: any) => {
      totalSale += item.categoryIncome;
      if (item.categoryIncome) {
        totaldetails += `类别：${item.categoryName},${item.categoryName}总销售：${item.categoryIncome};`;
      }
    });
    weekData.forEach((item: any) => {
      weekSale += item;
    });
    monthData.forEach((item: any) => {
      monthSale += item;
    });
    const saleText = `总销售：${totalSale}元;\n销售明细为-${totaldetails};\n本周销售总额：${weekSale}元；\n本月总销售额：${monthSale}元`;
    return {
      content: [
        {
          type: "text",
          text: saleText,
        },
      ],
    };
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text:
            "获取销售数据失败：" +
            error +
            "token为：" +
            token +
            "错误报告：" +
            JSON.stringify(aaa),
        },
      ],
    };
  }
};
